<template>
  <div class="content_box">
    <div class="lanmu_box">
      <div class="box">
        <van-tabs ref="tabs" v-model="active" @change="tabChange">
          <van-tab :title="item.name" v-for="(item,index) in goodTypeList" :key="index"></van-tab>
        </van-tabs>
      </div>
    </div>
    <div class="list_box" v-if="list.length">
      <div class="box" v-for="(item,index) in list" :key="index">
        <p class="label" :class="{'on': !item.putaway}">{{ item.putaway === 1?'上架中':'已下架' }}</p>
        <div class="img">
          <van-image
            width="100%"
            height="180rpx"
            fit="cover"
            lazy-load
            radius="10rpx"
            :src="item.img"
          />
        </div>
        <div class="msg">
          <p class="name">{{item.name}}</p>
          <p>单价：<span>{{item.price / 100}}元</span></p>
          <p>成本价	：<span>{{item.costPrice}}元</span></p>
          <p>销量：{{item.salesVolume}}个</p>
          <p @click.stop="">库存数量：<input type="text" v-model="item.stockNumber" @blur="blurStockNumber(item.stockNumber, item.id)" />个</p>
        </div>
        <van-icon @click="clickCz(item)" style="margin-left:auto" color="#999" name="arrow" />
      </div>
  
    </div>
    <div class="no_data" v-else>
      <img src="/static/images/no_sp.png" alt="">
      <p class="text">暂无商品列表</p>
    </div>
    <!-- 弹窗 -->
    <div class="pop_box">
      <van-popup position="bottom" :show="isAdmin" @close="closePop">
        <div class="box">
          <div class="title">
            <p>商品操作</p>
          </div>
          <div class="list">
            <p class="name">编辑商品</p>
            <p class="icon" @click="toEdit(true)">
              编辑
            </p>
          </div>
          <div class="list" @click="del">
            <p class="name">删除商品</p>
            <p class="icon">
              删除
            </p>
          </div>
          <p class="line"></p>
          <p class="quxiao" @click="isAdmin = false">取消</p>
        </div>
      </van-popup>
    </div>
    <div class="anniu_box" :style="{paddingBottom:isIphoneXnum+'px'}">
      <div class="box" :style="{paddingBottom:isIphoneXnum+'px'}">
        <p class="anniu" @click="toEdit(false)"><van-icon name="plus" /> 新增商品</p>
      </div>
    </div>
  </div>
</template>
 
<script>
import {
  getGoodsPage,
  delGoods,
  getGoodsTypeListAll,
  updateGoodsStock
} from "@/api/index";
export default {
  components: {},
  data() {
    return {
      list:[],
      id:'',
      isAdmin:false,
      isIphoneXnum:false,
      active:0,
      param:{
        current:1,
        size:100,
        goodsTypeId:null
      },
      goodTypeList:[]
    };
  },
  onShow() {
    this.getGoodsTypeAll()
  },

  methods: {

    async blurStockNumber(stockNumber, id){
      console.log(stockNumber, id)
      await updateGoodsStock({goodsId:id,number:stockNumber}).then(res=>{
        if(res.statusCode == '00000'){
          this.$toast('库存修改成功')
          this.getList()
        }else{
          this.$toast(res.message)
        }
      })
    },

    async getGoodsTypeAll(){
      await getGoodsTypeListAll().then(res=>{
        if(res.statusCode == '00000'){
          this.goodTypeList = res.data
          if (this.active) {
            this.param.goodsTypeId = this.goodTypeList[this.active].id
          } else {
            this.param.goodsTypeId = this.goodTypeList[0].id
          }
        }
      })
      this.getList()
    },

    // 状态切换
    tabChange(e){
      wx.pageScrollTo({
        scrollTop: 0,
      });
      let index = e.mp.detail.index
      this.active = index
      let id = this.goodTypeList[index].id
      this.param.current = 1
      this.param.goodsTypeId = id
      this.getList()
    },

    // 关闭弹窗
    closePop(){
      this.isAdmin = false
    },

    // 操作弹窗
    clickCz(item){
      this.isAdmin = true
      this.id = item.id
    },
    toEdit(state){
      this.isAdmin = false
      if(state){
        wx.navigateTo({
          url: `/pages/home/commodityAdmin/edit/main?id=${this.id}`,
        });
        return
      }
      wx.navigateTo({
        url: `/pages/home/commodityAdmin/edit/main`,
      });
    },

    // 商品列表
    async getList(){
      this.$showLoading('加载中...')
      await getGoodsPage(this.param).then(res=>{
        if(res.statusCode == '00000'){
          this.list = []
          this.list = res.data.records
          wx.hideLoading()
          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();
        }else{
          wx.hideLoading()
          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();
        }
      })
    },

    // 删除优惠券
    del(){
      wx.showModal({
        title: "温馨提示",
        content: "确定删除该条数据~",
        confirmText:'确认删除',
        success: (res)=>{
          if (res.confirm) {
            delGoods({id:this.id}).then(res=>{
              if(res.statusCode == '00000'){
                this.isAdmin = false
                this.getList()
              }else{
                this.$toast(res.message)
              }
            })
          }
        },
      });
    },
  },
  onLoad(options) {
    console.log(options)
    //判断手机机型，动态给底部添加样式
    wx.getSystemInfo({
      success: (res) => {
        console.log("手机信息res" + res.model);
        let modelmes = res.model;
        if (
          modelmes.search("iPhone 13") != -1 ||
          modelmes.search("iPhone 12") != -1 ||
          modelmes.search("iPhone 11") != -1 ||
          modelmes.search("iPhone X") != -1 ||
          modelmes.search("iPhone XR") != -1 ||
          modelmes.search("iPhone XS") != -1
        ) {
          this.isIphoneXnum = 25;
        } else {
          this.isIphoneXnum = 0;
        }
      },
    });
  },
  onHide() {},

  onUnload() {},
};
</script>

<style lang="scss" scoped>
.lanmu_box{
    height: 88rpx;
    margin-bottom:20rpx;
    .box{
      position: fixed;
      top:0;
      width: 100%;
      z-index: 100;
      background: #fff;
    }
    /deep/ .van-tab{
      color:#999;
      font-size:30rpx;
    }
    /deep/ .van-tabs__line{
      background: #222;
    }
    /deep/ .van-tab--active{
      color:#222;
      font-weight: bold;
    }
  }
.list_box{
    .box{
      margin:30rpx 30rpx 40rpx 30rpx;
      background: #fff;
      padding:24rpx;
      box-shadow: 0rpx 10rpx 20rpx 4rpx rgba(139, 129, 114, 0.1);
      border-radius: 20rpx;
      display: flex;
      position: relative;
      .label {
        position: absolute;
        top:20rpx;
        right:20rpx;
        padding:4rpx 12rpx;
        background: rgba(30, 104, 255, 0.1);
        color:#1E68FF;
        font-size:24rpx;
        border-radius: 12rpx;
        z-index: 10;
        &.on{
          background: rgba(153, 153, 153, 0.1);
          color:#999;
        }
      }
      .img{
        width: 180rpx;
      }
      .msg{
        margin-left:20rpx;
        p{
          margin-bottom:10rpx;
          font-size:26rpx;
          display: flex;
          input {
            border:1px solid #ccc;
            width: 100rpx;
          }
          span{
            color:#1E68FF;
          }
          &:last-child{
            margin-bottom:0;
          }
        }
        .name{
            font-weight: bold;
            color:#000;
            font-size:30rpx
          }
      }
    }
  }
.anniu_box{
  height: 150rpx;
  width: 100%;
  background: #fff;
  
  .box{
    position: fixed;
  bottom:0;
  left: 0;
  z-index: 10;
    width: 92%;
    height: 150rpx;
    line-height: 150rpx;
    padding:0 30rpx;
    background: #fff;
    display: flex;
    align-items: center;
    .anniu{
      box-shadow: 0rpx 10rpx 20rpx #CEDBFA;
      border:none;
      width: 100%;
      height: 88rpx;
      line-height: 88rpx;
      background: #1E68FF;
      border-radius: 100rpx;
      text-align: center;
      color: #fff;
      font-size: 30rpx;
    }
  }
}
.no_data{
  text-align: center;
  font-size:28rpx;
  color:#333;
  padding-top:150rpx;
  .text{
    font-weight: 500;
    color:#999;
  }
  img{
    width: 248rpx;
    height: 248rpx;
  }
}
.pop_box{
  /deep/ .van-popup {
    border-top-right-radius: 20rpx !important;
    border-top-left-radius: 20rpx !important;
  }
  .box{
    .title{
      height: 104rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      color:#323233;
      font-size:34rpx;
      border-bottom:1px solid #f0f0f0;
      position: relative;
      .tb{
        position: absolute;
        right:30rpx;
      }
    }
    .list{
      height: 104rpx;
      border-bottom:1px solid #f5f5f5;
      padding:0 40rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .name{
        font-size:32rpx;
        color:#323233;
      }
      .icon{
        width: 196rpx;
        height: 60rpx;
        display:flex;
        align-items: center;
        justify-content: center;
        border:1px solid #222;
        color:#222;
        font-size:28rpx;
        font-weight: 500;
        border-radius: 50rpx;
        &.on{
          opacity: 0.5;
        }
      }
      div{
        display: flex;
      }
      .tips{
        font-size:24rpx;
        color:red;
        margin-top:10rpx;
      }
      img{
        width: 32rpx;
        height: 32rpx;
        margin-right:10rpx;
      }
    }
    .line{
      width: 100%;
      height: 16rpx;
      background: #f8f8f8;
    }
    .quxiao{
      height: 100rpx;
      line-height: 100rpx;
      font-size:32rpx;
      color:#323233;
      text-align: center;
    }
  }
}
</style>